package com.foreveross.SYWebService;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.common.Constants;
import com.foreveross.common.Exception.ExceptionConstant;
import com.foreveross.common.Exception.HttpClientException;
import com.foreveross.common.Exception.SYHttpclientCallException;
import com.foreveross.util.HttpUtil;
import com.foreveross.util.JSONUtil;
import com.foreveross.util.ObjectToMap;
import com.foreveross.util.XmlUtil;
import com.google.gson.reflect.TypeToken;


public class SYHttpclientCall {
	private static Logger log = Logger.getLogger(SYHttpclientCall.class);
    private static String namespace = "http://tempuri.org/";
    private static String methodName = "GetSYS86Service";
  //  private static String wsdlLocation = "http://szsya.gicp.net:9090/NetApp/SYS86Service.asmx?wsdl";
   // private static String wsdlLocation = "http://szsya.gicp.net:9090/NetApp/SYS86Service.asmx/GetSYS86Service";
    private static String wsdlLocation = Constants.SYWebService_WsdlLocation;
    
    public static SYResult getSYData(Map<String, String> params){
    	SYResult syResult = null;
    	try{
    		String jsonStr = invoke(params);
    		System.out.println(jsonStr);
    		String validate_MSG = vaildateData(jsonStr);
        	syResult =  JSONUtil.jsonToClazz(jsonStr,new TypeToken<SYResult>(){});
    	}catch(HttpClientException e){
    		SYHttpclientCallException.throwSYHttpclientCallException(e);
    	}catch(SYHttpclientCallException e){
    		throw e;
    	}catch(Exception e){
    		SYHttpclientCallException.throwSYHttpclientCallException(e);
    	}
    	return syResult;
    }
    
    private static String invoke(Map<String, String> params) throws Exception {
        String content = HttpUtil.Post(wsdlLocation, params);
        String str = XmlUtil.parseXmlByStr(content);
        return str;
    }

    private static String buildRequestData(ConditionVo conditionVo) throws Exception {
        StringBuffer soapRequestData = new StringBuffer();
        soapRequestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        soapRequestData
                .append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
                        + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
                        + " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
        soapRequestData.append("<soap12:Body>");
        
        soapRequestData.append("<" + methodName + " xmlns=\"" + namespace
                + "\">");

        Field[] fields = conditionVo.getClass().getDeclaredFields();
        for (Field filed : fields) {
        	filed.setAccessible(true);
        	if(null != filed.get(conditionVo)){
        		soapRequestData.append("<" + filed.getName() + ">" +filed.get(conditionVo)
                        + "</" + filed.getName() + ">");
        	}
        }
        
        soapRequestData.append("</" + methodName + ">");
        
        soapRequestData.append("</soap12:Body>");
        soapRequestData.append("</soap12:Envelope>");

        return soapRequestData.toString();
    }
    
    /**
     * 验证思源接口数据内容
     * @param jsonStr
     */
    private static String vaildateData(String jsonStr){
    	String errorStr = "_ERROR";
    	int errorIndex = jsonStr.indexOf(errorStr);
    	if(errorIndex > -1){
    		SYHttpclientCallException.throwSYHttpclientCallERRORException();
    	}else{
    	   	String splitStr ="[{\"Syswin\":[";
    		int indexStart = jsonStr.indexOf(splitStr)+splitStr.length();
    		String endStr ="]}]}";;
    		int indexEnd = jsonStr.length()-endStr.length();
    		String content = jsonStr.substring(indexStart, indexEnd);
    		
    		if(content.length() == 0){
    			errorStr = "_NULL";
//    			SYHttpclientCallException.throwSYHttpclientCallNULLException();
    		} else {
    			errorStr = "_SUCCESS";
    		}
    	}
		return errorStr;
    }
    

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        Map<String, String> patameterMap = new HashMap<String, String>();
		
		ConditionVo conditionVo = new ConditionVo();
		conditionVo.setP0("UserRev_OwnerVerify");
		conditionVo.setP1("王霞");
		conditionVo.setP2("13800010009");
		conditionVo.setP3("00100020090900000003");
/*		conditionVo.setP1("方雪梅");
		conditionVo.setP2("13602669829");
		conditionVo.setP3("13113120110400000003");*/
		
//		conditionVo.setP0("UserRev_GetFeeList");
//		conditionVo.setP1("00100120091000000007");
		
		
		patameterMap = ObjectToMap.objectToMapViaFields(conditionVo,true);
		SYResult sysResult =  getSYData(patameterMap);
		System.out.println("sysResult:"+JSONUtil.toJsonWithGson(sysResult));
       //System.out.println(sysResult.getUserRev_OwnerVerify().get(0).getSyswin().get(0).getProjectID());
/*       List<DataVo> datoVoList = sysResult.getUserRev_OwnerVerify().get(0).getSyswin();
       for(DataVo dataVo : datoVoList){
    	   System.out.println(JSONUtil.toJsonWithGson(dataVo));
       }*/
    }

}